package com.test.demo.base;

import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;


public abstract class BaseBottomFragment extends Fragment {

    /** 父视图 */
    protected View viewParent;
    /** 广播接收器 */
    protected BroadcastReceiver receiver;
    /** 广播过滤器 */
    protected IntentFilter filter;

    /** fragment里的所有传递的数据都可以拿 */
    protected Bundle getBundle = null;
    /** 用来传值的bundle */
    protected Bundle postBundle = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewParent = View.inflate(getActivity(), getContentViewId(), null);

        getBundle = getArguments();
        postBundle = new Bundle();
        try {
            findViews();
            initGetData();
            widgetListener();
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (viewParent.getParent() != null) {
            ((ViewGroup) viewParent.getParent()).removeView(viewParent);
        }
        return viewParent;
    }

    protected abstract int getContentViewId();

    protected abstract void findViews();

    public abstract void initGetData();


    protected abstract void init();

    protected abstract void widgetListener();

//    private void resetView() {
//        // getViews();
//        viewParent = View.inflate(getActivity(), getContentViewId(), null);
//        findViews();
//        initGetData();
//        widgetListener();
//        init();
//    }

    public boolean isCreated() {
        return viewParent != null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @SuppressWarnings("unchecked")
    protected <T extends View> T findViewByIds(int id) {
        if (viewParent == null) {
            viewParent = View.inflate(getActivity(), getContentViewId(), null);
        }
        return (T) viewParent.findViewById(id);
    }
}